package com.feinno.urcs.group.service;

import com.feinno.database.DataTable;
import com.feinno.database.Database;
import com.feinno.redis.RedisClient;
import com.feinno.urcs.group.api.AndPcStatusService;
import com.feinno.urcs.types.MobileNo;
import com.feinno.util.StringUtils;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.util.Collection;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
@ServiceImplementation
public class AndPcStatusServiceImpl implements AndPcStatusService {
	private static final Logger LOGGER = LoggerFactory.getLogger(AndPcStatusServiceImpl.class);
	//TODO 后续可能需要增加对GroupType 的 判断.
////	@FieldSetter("${PC_CATS_DB}")
////	private Database pcCatsDB;
//
//
//	@FieldSetter("${PC_ACTIVE_RD}")
//	private RedisClient redis;


//	@Override
//	public int getAndPcUserId(MobileNo mobileNo) throws Exception {
//
//		if (mobileNo == null) return -1;
//		String sql = "SELECT UserId,MobileNo FROM [CATDB].[dbo].[CAT_UserCatalogAll]  WHERE MobileNo=?";
//		DataTable dt = pcCatsDB.executeTable(sql, mobileNo.getMobile());
//		int userId = -1;
//		if (dt.getRowCount() > 0) {
//			userId = dt.getRow(0).getInt("UserId");
//		}
//
//		return userId;
//	}

	@Override
	public int getUserMessageActive(MobileNo mobileNo) throws Exception {

		int activeint = 2;
//		int userId = -1;
//		try {
//			userId = getAndPcUserId(mobileNo);
//
//			if (userId == -1) {
//				LOGGER.error(String.format("getUserMessageActive userId=-1 mobile=%s", mobileNo));
//				throw new RuntimeException(String.format("getUserMessageActive userId=-1 mobile=%s", mobileNo));
//			}
//			Collection<Jedis> jedises = redis.getJedis().getAllShards();
//			String avtive = null;
//			for (Jedis jedis : jedises) {
//				if (jedis != null) {
//					jedis.select(6); // TODO  先这么写了. 现在的组件不支持初始化指定DB PC那边的配置DB 是6
//					avtive = jedis.get(String.valueOf(userId));
//					if (!StringUtils.isNullOrEmpty(avtive)) break;
//				}
//			}
//			activeint = Integer.parseInt(avtive);
//		} catch (Exception e) {
//			LOGGER.error(String.format("avtive error mobile=%s pcUserid=%s", mobileNo, userId));
//			activeint = 2;
//		}
		return activeint;
	}
}
